var util = require('../../utils/util.js');
var ajax = require('../../utils/ajax.js');
var common = require('../../utils/common.js');
var PageBase = require('../../common/PageBase.js');
var Pagination = require('../../common/pagination/index.js');
var app = getApp();

Page(common.extendPage(PageBase, {
    data: {
        dataLoaded: false,
        shopName: ''
    },

    onLoad: function(options) {
        // this._options = options;
        this._options = util.parseOptions(options);

        common.initApp({
            extSuc: () => {
                this._getOrderList();
            },
            loginSuc: () => {
                this.$loginSuc();
            },
            sysInfoSuc: () => {
                this.setData({
                    winHei: app.getWinHei()
                });
            }
        });
    },

    onPullDownRefresh() {
        this._pagination && this._pagination.flush(() => {
            wx.stopPullDownRefresh();
        });
    },

    onReachBottom() {
        this._pagination && this._pagination.loadNext();
    },

    /**
     * 获取订单列表
     */
    _getOrderList: function() {
        var {
            shopId
        } = this._options;

        this._pagination = new Pagination({
            page: this,
            scope: 'pagination'
        }).init({
            rendData: (page, cb) => {
                ajax.G({
                    url: '/market/Pay/payOrder',
                    data: {
                        shop_id: shopId,
                        page: page
                    },
                    success: (res) => {
                        var {
                            order_list: orderList = [],
                            shop_title
                        } = res.result;
                        var allList = this.data.orderList || [];

                        this.setData({
                            dataLoaded: true,
                            orderList: page == 1 ? orderList : allList.concat(orderList),
                            shopName: shop_title
                        });

                        cb && cb(orderList.length);
                    }
                });
            }
        });
    },


}));